package com.ta.gulimall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ta.gulimall.member.service.MemberLevelService;
import com.ta.gulimall.member.view.entity.MemberLevelEntity;
import com.ta.gulimall.member.view.vo.MemberLoginVo;
import com.ta.gulimall.member.view.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.Query;

import com.ta.gulimall.member.dao.MemberDao;
import com.ta.gulimall.member.view.entity.MemberEntity;
import com.ta.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelService memberLevelService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(MemberRegisterVo vo) {
        //验证用户昵称唯一性
        checkUsername(vo.getUsername());
        //检查手机是否使用
        checkMobile(vo.getPhone());

        MemberLevelEntity memberLevel = memberLevelService.getDefaultMemberLevel();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode(vo.getPassword());

        MemberEntity member = new MemberEntity()
                .setUsername(vo.getUsername())
                .setNickname(vo.getUsername())
                .setPassword(password)
                .setMobile(vo.getPhone())
                .setLevelId(memberLevel.getId());

        save(member);
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<MemberEntity>()
                .eq(MemberEntity::getUsername, vo.getUsername()).or().eq(MemberEntity::getMobile, vo.getUsername());
        MemberEntity member = getOne(wrapper);
        if (member == null) {
            throw new RuntimeException("没有该用户的信息，请注册");
        }

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean matches = passwordEncoder.matches(vo.getPassword(), member.getPassword());
        if (!matches) {
            throw new RuntimeException("密码错误，请验证后重新输入");
        }

        return member;

    }

    void checkUsername(String username) {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<MemberEntity>()
                .eq(MemberEntity::getUsername, username);
        int count = count(wrapper);
        if (count != 0) {
            throw new RuntimeException("用户昵称已重复");
        }
    }

    void checkMobile(String phone) {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<MemberEntity>()
                .eq(MemberEntity::getMobile, phone);
        int count = count(wrapper);
        if (count != 0) {
            throw new RuntimeException("手机号码已被使用");
        }
    }

}